#ifndef __PCSC_DESFIRE_H__
#define __PCSC_DESFIRE_H__

#include <winscard.h>

#ifndef SPROX_DESFIRE_LIB
  #define SPROX_DESFIRE_LIB __declspec( dllimport )
#endif
#ifndef SPROX_DESFIRE_API
  #define SPROX_DESFIRE_API __cdecl
#endif


typedef unsigned char   BYTE;
typedef unsigned short  WORD;
typedef unsigned long   DWORD;
typedef signed short    SWORD;    
typedef signed long     SDWORD;


#include "desfire_card.h"



#ifdef __cplusplus
extern  "C"
{
#endif

/*
 * Library entry points
 * --------------------
 */
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_AttachLibrary(SCARDHANDLE hCard);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_DetachLibrary(SCARDHANDLE hCard);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_IsoWrapping(SCARDHANDLE hCard, BYTE mode);


/*
 * Library helpers
 * ---------------
 */

SPROX_DESFIRE_LIB const TCHAR *SPROX_DESFIRE_API SCardDesfire_GetErrorMessage(LONG status);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ExplainDataFileSettings(const BYTE additionnal_settings_array[],
                                                                                 DWORD *eFileSize);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ExplainValueFileSettings(const BYTE additionnal_settings_array[],
                                                                                  SDWORD *lLowerLimit,
                                                                                  SDWORD *lUpperLimit,
                                                                                  DWORD *eLimitedCredit,
                                                                                  BYTE *bLimitedCreditEnabled);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ExplainRecordFileSettings(const BYTE additionnal_settings_array[],
                                                                                   DWORD *eRecordSize,
                                                                                   DWORD *eMaxNRecords,
                                                                                   DWORD *eCurrNRecords);

/*
 * Desfire EV0 functions
 * ---------------------
 */

SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_Authenticate(SCARDHANDLE hCard,
                                                                   BYTE bKeyNumber,
                                                                   const BYTE pbAccessKey[16]);                                                                  
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ChangeKeySettings(SCARDHANDLE hCard,
                                                                           BYTE key_settings);                                                                       
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetKeySettings(SCARDHANDLE hCard,
                                                                        BYTE *key_settings,
                                                                        BYTE *key_count);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ChangeKey(SCARDHANDLE hCard,
                                                                BYTE key_number,
                                                                const BYTE new_key[16],
                                                                const BYTE old_key[16]);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetKeyVersion(SCARDHANDLE hCard,
                                                                       BYTE bKeyNumber,
                                                                       BYTE *pbKeyVersion);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_FormatPICC(SCARDHANDLE hCard);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateApplication(SCARDHANDLE hCard,
                                                                        DWORD aid,
                                                                        BYTE key_setting_1,
                                                                        BYTE key_setting_2);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_DeleteApplication(SCARDHANDLE hCard,
                                                                           DWORD aid);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetApplicationIDs(SCARDHANDLE hCard,
                                                                           BYTE aid_max_count,
                                                                           DWORD aid_list[],
                                                                           BYTE *aid_count);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_SelectApplication(SCARDHANDLE hCard,
                                                                           DWORD aid);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetVersion(SCARDHANDLE hCard,
                                                                    DF_VERSION_INFO *pVersionInfo);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetFileIDs(SCARDHANDLE hCard,
                                                                    BYTE fid_max_count,
                                                                    BYTE fid_list[],
                                                                    BYTE *fid_count);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetFileSettings(SCARDHANDLE hCard,
                                                                         BYTE file_id,
                                                                         BYTE *file_type,
                                                                         BYTE *comm_mode,
                                                                         WORD *access_rights,
                                                                         DF_ADDITIONAL_FILE_SETTINGS *additionnal_settings);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ChangeFileSettings(SCARDHANDLE hCard,
                                                                            BYTE file_id,
                                                                            BYTE comm_mode,
                                                                            WORD access_rights);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateStdDataFile(SCARDHANDLE hCard,
                                                                           BYTE file_id,
                                                                           BYTE comm_mode,
                                                                           WORD access_rights,
                                                                           DWORD file_size);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateBackupDataFile(SCARDHANDLE hCard,
                                                                              BYTE file_id,
                                                                              BYTE comm_mode,
                                                                              WORD access_rights,
                                                                              DWORD file_size);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateValueFile(SCARDHANDLE hCard,
                                                                         BYTE file_id,
                                                                         BYTE comm_mode,
                                                                         WORD access_rights,
                                                                         SDWORD lower_limit,
                                                                         SDWORD upper_limit,
                                                                         SDWORD initial_value,
                                                                         BYTE limited_credit_enabled);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateLinearRecordFile(SCARDHANDLE hCard,
                                                                                BYTE file_id,
                                                                                BYTE comm_mode,
                                                                                WORD access_rights,
                                                                                DWORD record_size,
                                                                                DWORD max_records);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateCyclicRecordFile(SCARDHANDLE hCard,
                                                                                BYTE file_id,
                                                                                BYTE comm_mode,
                                                                                WORD access_rights,
                                                                                DWORD record_size,
                                                                                DWORD max_records);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_DeleteFile(SCARDHANDLE hCard,
                                                                    BYTE file_id);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ReadData(SCARDHANDLE hCard,
                                                                  BYTE file_id,
                                                                  BYTE comm_mode,
                                                                  DWORD from_offset,
                                                                  DWORD max_count,
                                                                  BYTE data[],
                                                                  DWORD *done_count);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ReadData2(SCARDHANDLE hCard,
                                                                   BYTE file_id,
                                                                   DWORD from_offset,
                                                                   DWORD max_count,
                                                                   BYTE data[],
                                                                   DWORD *done_count);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_WriteData(SCARDHANDLE hCard,
                                                                   BYTE file_id,
                                                                   BYTE comm_mode,
                                                                   DWORD from_offset,
                                                                   DWORD size,
                                                                   const BYTE data[]);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_WriteData2(SCARDHANDLE hCard,
                                                                    BYTE file_id,
                                                                    DWORD from_offset,
                                                                    DWORD size,
                                                                    const BYTE data[]);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetValue(SCARDHANDLE hCard,
                                                                  BYTE file_id,
                                                                  BYTE comm_mode,
                                                                  SDWORD *value);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetValue2(SCARDHANDLE hCard,
                                                                   BYTE file_id,
                                                                   SDWORD *value);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ReadRecords(SCARDHANDLE hCard,
                                                                     BYTE file_id,
                                                                     BYTE comm_mode,
                                                                     DWORD from_record,
                                                                     DWORD max_record_count,
                                                                     DWORD record_size,
                                                                     BYTE data[],
                                                                     DWORD *record_count);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ReadRecords2(SCARDHANDLE hCard,
                                                                      BYTE file_id,
                                                                      DWORD from_record,
                                                                      DWORD max_record_count,
                                                                      DWORD record_size,
                                                                      BYTE data[],
                                                                      DWORD *record_count);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_WriteRecord(SCARDHANDLE hCard,
                                                                     BYTE file_id,
                                                                     BYTE comm_mode,
                                                                     DWORD from_offset,
                                                                     DWORD size,
                                                                     const BYTE data[]);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_WriteRecord2(SCARDHANDLE hCard,
                                                                      BYTE file_id,
                                                                      DWORD from_offset,
                                                                      DWORD size,
                                                                      const BYTE data[]);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_LimitedCredit(SCARDHANDLE hCard,
                                                                       BYTE file_id,
                                                                       BYTE comm_mode,
                                                                       SDWORD amount);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_LimitedCredit2(SCARDHANDLE hCard,
                                                                        BYTE file_id,
                                                                        SDWORD amount);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_Credit(SCARDHANDLE hCard,
                                                                BYTE file_id,
                                                                BYTE comm_mode,
                                                                SDWORD amount);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_Credit2(SCARDHANDLE hCard,
                                                                 BYTE file_id,
                                                                 SDWORD amount);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_Debit(SCARDHANDLE hCard,
                                                               BYTE file_id,
                                                               BYTE comm_mode,
                                                               SDWORD amount);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_Debit2(SCARDHANDLE hCard,
                                                                BYTE file_id,
                                                                SDWORD amount);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ClearRecordFile(SCARDHANDLE hCard,
                                                                         BYTE file_id);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CommitTransaction(SCARDHANDLE hCard);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_AbortTransaction(SCARDHANDLE hCard);

/*
 * Desfire EV1 functions
 * ---------------------
 */

SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_Authenticate24(SCARDHANDLE hCard,
                                                                     BYTE bKeyNumber,
                                                                     const BYTE pbAccessKey[24]);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_AuthenticateAes(SCARDHANDLE hCard,
                                                                      BYTE bKeyNumber,
                                                                      const BYTE pbAccessKey[16]);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ChangeKey24(SCARDHANDLE hCard,
                                                                  BYTE key_number,
                                                                  const BYTE new_key[24],
                                                                  const BYTE old_key[24]);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_ChangeKeyAes(SCARDHANDLE hCard,
                                                                   BYTE key_number,
                                                                   BYTE key_version,
                                                                   const BYTE new_key[16],
                                                                   const BYTE old_key[16]);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetFreeMemory(SCARDHANDLE hCard,
                                                                    DWORD *pdwFreeBytes);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_SetConfiguration(SCARDHANDLE hCard,
                                                                       BYTE option,
                                                                       const BYTE data[],
                                                                       BYTE length);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetCardUID(SCARDHANDLE hCard,
                                                                 BYTE uid[7]);

/*
 * Desfire ISO-related functions
 * -----------------------------
 */
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateIsoApplication(SCARDHANDLE hCard,
                                                                           DWORD       aid,
                                                                           BYTE        key_settings,
                                                                           BYTE        keys_count,
                                                                           WORD        iso_df_id,
                                                                           const BYTE  iso_df_name[16],
                                                                           BYTE        iso_df_namelen);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetIsoDFNames(SCARDHANDLE hCard,
                                                                    BOOL       *follow_up,
                                                                    DWORD      *aid,
                                                                    WORD       *iso_df_id,
                                                                    BYTE        iso_df_name[16+1],
                                                                    BYTE       *iso_df_namelen);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_GetIsoFileIDs(SCARDHANDLE hCard,
                                                                    BYTE fid_max_count,
                                                                    WORD fid_list[],
                                                                    BYTE *fid_count);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateIsoStdDataFile(SCARDHANDLE hCard,
                                                                           BYTE file_id,
                                                                           WORD iso_ef_id,
                                                                           BYTE comm_mode,
                                                                           WORD access_rights,
                                                                           DWORD file_size);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateIsoBackupDataFile(SCARDHANDLE hCard,
                                                                              BYTE file_id,
                                                                              WORD iso_ef_id,
                                                                              BYTE comm_mode,
                                                                              WORD access_rights,
                                                                              DWORD file_size);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateIsoValueFile(SCARDHANDLE hCard,
                                                                         BYTE file_id,
                                                                         WORD iso_ef_id,
                                                                         BYTE comm_mode,
                                                                         WORD access_rights,
                                                                         SDWORD lower_limit,
                                                                         SDWORD upper_limit,
                                                                         SDWORD initial_value,
                                                                         BYTE limited_credit_enabled);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateIsoLinearRecordFile(SCARDHANDLE hCard,
                                                                                BYTE file_id,
                                                                                WORD iso_ef_id,
                                                                                BYTE comm_mode,
                                                                                WORD access_rights,
                                                                                DWORD record_size,
                                                                                DWORD max_records);
SPROX_DESFIRE_LIB LONG SPROX_DESFIRE_API SCardDesfire_CreateIsoCyclicRecordFile(SCARDHANDLE hCard,
                                                                                BYTE file_id,
                                                                                WORD iso_ef_id,
                                                                                BYTE comm_mode,
                                                                                WORD access_rights,
                                                                                DWORD record_size,
                                                                                DWORD max_records);



#ifdef __cplusplus
}
#endif


#endif
